<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业主姓名" prop="ownerName">
        <el-input
            v-model="queryParams.ownerName"
            placeholder="请输入业主姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主手机号" prop="ownerPhone">
        <el-input
            v-model="queryParams.ownerPhone"
            placeholder="请输入业主手机号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装区县代码" prop="regionCode">
        <el-input
            v-model="queryParams.regionCode"
            placeholder="请输入安装区县代码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装地址" prop="address">
        <el-input
            v-model="queryParams.address"
            placeholder="请输入安装地址"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applicationTime">
        <el-date-picker clearable
                        v-model="queryParams.applicationTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="安装进度" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择安装进度" clearable>
          <el-option
              v-for="dict in biz_install_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="子进度" prop="subState">
        <el-select v-model="queryParams.subState" placeholder="请选择子进度" clearable>
          <el-option
              v-for="dict in biz_install_sub_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="代理商" prop="agentId">
        <el-input
            v-model="queryParams.agentId"
            placeholder="请输入代理商"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理员工" prop="agentUserId">
        <el-input
            v-model="queryParams.agentUserId"
            placeholder="请输入处理员工"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="installList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="业主姓名" align="center" prop="ownerName"/>
      <el-table-column label="业主手机号" align="center" prop="ownerPhone"/>
      <el-table-column label="安装区县" align="center" prop="regionCode"/>
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装进度" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="biz_install_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="子进度" align="center" prop="subState">
        <template #default="scope">
          <dict-tag :options="biz_install_sub_state" :value="scope.row.subState"/>
        </template>
      </el-table-column>
      <el-table-column label="代理商" align="center" prop="agentId"/>
      <el-table-column label="处理员工" align="center" prop="agentUserId"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)"
                     v-hasPermi="['business:install:edit']">查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

  </div>
</template>

<script setup name="Install">
import {addInstall, delInstall, listInstall, updateInstall} from "@/api/business/install";

const {proxy} = getCurrentInstance();
const {biz_install_state, biz_install_sub_state} = proxy.useDict('biz_install_state', 'biz_install_sub_state');

const installList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    ownerName: null,
    ownerPhone: null,
    regionCode: null,
    address: null,
    applicationTime: null,
    state: null,
    subState: null,
    agentId: null,
    agentUserId: null,
  },
  rules: {
    ownerName: [
      {required: true, message: "业主姓名不能为空", trigger: "blur"}
    ],
    ownerPhone: [
      {required: true, message: "业主手机号不能为空", trigger: "blur"}
    ],
    regionCode: [
      {required: true, message: "安装区县代码不能为空", trigger: "blur"}
    ],
    applicationTime: [
      {required: true, message: "申请时间不能为空", trigger: "blur"}
    ],
    state: [
      {required: true, message: "安装进度不能为空", trigger: "change"}
    ],
    subState: [
      {required: true, message: "子进度不能为空", trigger: "change"}
    ],
    agentId: [
      {required: true, message: "代理商不能为空", trigger: "blur"}
    ],
    agentUserId: [
      {required: true, message: "处理员工不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询安装申请列表 */
function getList() {
  loading.value = true;
  listInstall(queryParams.value).then(response => {
    installList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    ownerName: null,
    ownerPhone: null,
    regionCode: null,
    address: null,
    applicationTime: null,
    state: null,
    subState: null,
    agentId: null,
    agentUserId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("installRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加安装申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id;
  router.push({path: "/install/detail/" + id});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["installRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInstall(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInstall(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除安装申请编号为"' + _ids + '"的数据项？').then(function () {
    return delInstall(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/install/export', {
    ...queryParams.value
  }, `install_${new Date().getTime()}.xlsx`)
}

getList();
</script>
